import { startAllServices } from "@services/start.service.wrapper";
import { serviceRegister } from "@services/service.register";
import { IncomingMessage } from "http";
import { WebSocketServer } from "ws";

const server = new WebSocketServer({ port: 8081 });
server.on("connection", (socket: WebSocket, request: IncomingMessage) => {
  console.log(
    `received connection request from: [${request.socket.remoteAddress}]`
  );

  socket.send(`connection request received, websocket connected`);

  request.on("close", () => {
    console.log(
      `[request.on("close")], client: [${request.socket.remoteAddress}] closed`
    );
  });

  socket.addEventListener("message", (event:MessageEvent)=>{
    console.log(`received message: ${event.data}, from: [${event.target}]`);
    socket.send(`received message: ${event.data}, this is the message from server side.`)
    
  });
});

server.on("close", (socket: WebSocket, request: IncomingMessage) => {
  console.log(
    `[server.on("close")], client: [${request.socket.remoteAddress}] closed`
  );
});

function onMessage(message: MessageEvent) {
  

  
}

function startService() {
  console.log("********** starting services **********")
  const register = serviceRegister;

  setTimeout(() => {
    startAllServices();
  }, 200);
}

function startWsServer(){

}

startService();
startWsServer();